package com.sinoatma.physignsmonitor.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import android.widget.EditText
import android.widget.RadioButton
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.hjq.toast.ToastUtils
import com.sinoatma.physignsmonitor.R
import kotlinx.coroutines.Job

object DialogUploadInfoFailedUtil {
    private var currentDialog: MaterialDialog? = null
    enum class DialogType {
        INFO
    }
    fun showCustomDialogWithCountdown(context: Context, dialogType: DialogType,msg:String,
        onPositiveButtonClick: (() -> Unit)? = null,
        onNegativeButtonClick: (() -> Unit)? = null): MaterialDialog {
        currentDialog?.takeIf { it.isShowing }?.let { return it }
        val dialog = MaterialDialog(context).show {
            customView(R.layout.dialog_material_info_layout).maxWidth(com.common.frame.R.dimen.dp_875)
            cancelable(false)
            setOnDismissListener {
                currentDialog = null
            }
        }
        val window: Window = dialog!!.window!!
        //设置弹出位置
        window.setGravity(Gravity.CENTER)
        //设置弹出动画
        // 将对话框的大小按屏幕大小的百分比设置
        val windowManager = window.windowManager
        val display = windowManager.defaultDisplay
        val lp: WindowManager.LayoutParams = dialog!!.window!!.attributes
        //横屏尺寸
        lp.height = (display.height * 0.55).toInt()
        lp.width = (display.width * 0.4).toInt()
        dialog!!.window?.attributes = lp

        dialog.view.background = context.getDrawable(R.drawable.dialog_material_bg)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val customView = dialog.getCustomView()
        val tv_content = customView.findViewById<TextView>(R.id.tv_content)
        tv_content.text="${tv_content.text.toString()}  ${msg}"
        // 设置“确定”按钮的文本和点击事件
        val confirmButton = customView.findViewById<TextView>(R.id.btn_confirm)

        confirmButton.apply {
            setOnClickListener {
                dialog.dismiss()
                onPositiveButtonClick?.invoke()
            }
        }
        // 设置“取消”按钮的文本和点击事件
        val cancelButton = customView.findViewById<TextView>(R.id.btn_clean)
        cancelButton.apply {
            setOnClickListener {
                dialog.dismiss()
                onNegativeButtonClick?.invoke()
            }
        }
        // 设置对话框类型样式
        when (dialogType) {
            DialogType.INFO -> {
                // 可设置信息样式，例如背景颜色或图标
            }
        }
        currentDialog=dialog
        return dialog
    }
}
